package com.ww.disruptor.handler;

import com.lmax.disruptor.WorkHandler;
import com.ww.disruptor.event.Order;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static com.ww.disruptor.consumer.WorkHandlerDemo05.totalCountDownLatch;

/**
 * TODO
 *
 * @author wanggw
 * @date 2024-09-15 13:34:08
 */
public class Consumer implements WorkHandler<Order> {

    private String consumerId;

    public String getConsumerId() {
        return consumerId;
    }

    private AtomicInteger count = new AtomicInteger(0);

    private Random random = new Random();

    public Consumer(String consumerId) {
        this.consumerId = consumerId;
    }

    @Override
    public void onEvent(Order order) throws Exception {
        // 模拟消费时间
        TimeUnit.MILLISECONDS.sleep(random.nextInt(5));

        count.incrementAndGet();

        //
        totalCountDownLatch.countDown();
    }

    public int getCount() {
        return count.get();
    }
}
